package com.whu.MenuHub.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whu.MenuHub.dao.UserFlavorDao;
import com.whu.MenuHub.domain.UserFlavor;
import com.whu.MenuHub.exception.MenuHubException;
import com.whu.MenuHub.service.UserFlavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserFlavorServiceImpl extends ServiceImpl<UserFlavorDao, UserFlavor> implements UserFlavorService {
    @Autowired
    UserFlavorDao userFlavorDao;

    //获得一条口味偏好信息
    @Cacheable(cacheNames = "userflavor",key = "#id",condition = "#id!=null")
    @Override
    public UserFlavor getfalvorByUserID(Integer id){
        return userFlavorDao.selectById(id);
    }

    //新增一条口味偏好信息
    @Override
    public UserFlavor insertUserFalvor(UserFlavor userFlavor) throws MenuHubException {
        try{
            userFlavorDao.insert(userFlavor);
            return userFlavor;
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.INSERT_ERROR,"添加用户口味偏好信息失败");
        }
    }

    //修改一条口味偏好信息
    @Transactional(rollbackFor = MenuHubException.class)
    @Override
    @CacheEvict(cacheNames = "userflavor",key = "#userFlavor.userID")
    public void updateuserFlavor(UserFlavor userFlavor) throws MenuHubException{
        try{
            userFlavorDao.updateById(userFlavor);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.UPDATE_ERROR,"修改用户口味偏好信息失败");
        }
    }

    //删除一条口味偏好信息
    @Transactional(rollbackFor = MenuHubException.class)
    @Override
    @CacheEvict(cacheNames = "userflavor",key = "#id")
    public void deleteUserFlavorById(Integer id) throws MenuHubException{
        try{
            userFlavorDao.deleteById(id);
        }catch (Exception e){
            throw new MenuHubException(MenuHubException.ErrorType.DELETE_ERROR,"删除用户口味偏好信息失败");
        }
    }

}
